﻿namespace BootstrapAdmin.Web.Core.Utils;

public static class ObjectExtensions
{
    public static T To<T>(this object? obj)
        where T : struct
    {
        if (obj == null)
        {
            return default;
        }

        return (T)obj;
    }

    public static T As<T>(this object? obj, T defaultValue = default)
        where T : struct
    {
        try
        {
            if (obj == null)
            {
                return defaultValue;
            }

            return (T)obj;
        }
        catch
        {
            return defaultValue;
        }
    }
}
